﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using CharacterCreation.Business.Repositories;
using CharacterCreation.Business.Repositories.Contracts;
using CharacterCreation.Business.ViewModels;
using CharacterCreation.Business;
using CharacterCreation.Business.Calculations;

namespace CharacterCreation.Web.Controllers
{
    public class CharacterController : Controller
    {
        private ICharacterRepository repository;

        public CharacterController(){
            repository = (ICharacterRepository)RepositoryFactory.Create<VMCharacter>();
        }

        public ActionResult Index(){
            return View(repository.Get());
        }

        public ActionResult Create()
        {
            ViewBag.LeftStats = CharacterCreation.Business.Calculations.Dice.Statistics(155);
            ViewBag.LeftStatTotal = ((List<int>)ViewBag.LeftStats).Sum();
            ViewBag.RightStats = CharacterCreation.Business.Calculations.Dice.Statistics(155);
            ViewBag.RightStatTotal = ((List<int>)ViewBag.RightStats).Sum();

            ViewBag.IsValid = true;
            ViewBag.ShowRolledStats = true;
            VMCharacter character = VMCharacter.Create();
            return View(character);
        }

        [HttpPost]
        public ActionResult Create(VMCharacter character)
        {
            ViewBag.IsValid = ModelState.IsValid;
            ViewBag.ShowRolledStats = false;
            if (ModelState.IsValid) repository.Add(character);
            return RedirectToAction("Details", new { id = character.Id });
        }

        public ActionResult Details(Guid id)
        {
            VMCharacter character = repository.Get(id);
            character.Statistics = character.Load(c => c.Statistics).OrderBy(s => s.OrderIndex).ToList();
            character.Skills = character.Load(c => c.Skills).OrderBy(s => s.Name).ToList();
            return View(character);
        }

        [HttpPost]
        public ActionResult Details(VMCharacter character)
        {
            ViewBag.IsValid = ModelState.IsValid;
            ViewBag.ShowRolledStats = false;
            if (ModelState.IsValid) repository.Update(character);
            return View(character);
        }

        public JsonResult GetRace(string raceId)
        {
            var r = RepositoryFactory.Create<VMRace>();
            VMRace race = r.Get(Guid.Parse(raceId));
            
            return Json(race);
        }

        public ActionResult StatisticDetail(Guid characterStatisticId)
        {
            //Load all the xp details for this statistic detail
            VMCharacterStatistic characterStatistic = RepositoryFactory.Create<VMCharacterStatistic>().Get(characterStatisticId);
            ViewBag.XPDetails = characterStatistic.Load(c => c.XPDetails);
            ViewBag.CharacterId = characterStatistic.CharacterId;
            
            //Create a new xp detail the user can fill out
            var xpDetail = new VMCharacterStatisticXPDetail() {
                Id = Guid.NewGuid(),
                CharacterId = characterStatistic.CharacterId,
                CharacterStatisticId = characterStatisticId,
                DateReceived = DateTime.Now,
            };
            return View(xpDetail);
        }

        [HttpPost]
        public ActionResult StatisticDetail(VMCharacterStatisticXPDetail xpDetail)
        {
            RepositoryFactory.Create<VMCharacterStatisticXPDetail>().Add(xpDetail);
            RepositoryFactory.Create<VMCharacterStatistic>().Get(xpDetail.CharacterStatisticId).Update();
            return RedirectToAction("StatisticDetail", new { xpDetail.CharacterStatisticId });
        }

        public ActionResult DeleteStatisticDetail(Guid characterStatisticXPDetailId, Guid characterStatisticId)
        {
            RepositoryFactory.Create<VMCharacterStatisticXPDetail>().Delete(characterStatisticXPDetailId);
            RepositoryFactory.Create<VMCharacterStatistic>().Get(characterStatisticId).Update();
            return RedirectToAction("StatisticDetail", new { characterStatisticId });
        }

        public ActionResult AddSkill(Guid characterId)
        {
            VMCharacterSkill skill = new VMCharacterSkill()
            {
                Id = Guid.NewGuid(), CharacterId = characterId
            };

            return View(skill);
        }

        [HttpPost]
        public ActionResult AddSkill(VMCharacterSkill skill)
        {
            RepositoryFactory.Create<VMCharacterSkill>().Add(skill);
            return RedirectToAction("Details", new { id = skill.CharacterId });
        }

        public ActionResult CharacterSkillDetail(Guid characterSkillId)
        {
            return View();
        }

    }
}
